package com.newasia.management.services;/**
 * Created by mic on 2019/7/30.
 */


import com.newasia.commons.tool.exception.ValidateException;
import com.newasia.management.IManagementSpecService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.converter.ItemSpecConverter;
import com.newasia.management.dal.item.entitys.ItemSpec;
import com.newasia.management.dal.item.entitys.ItemSpecExample;
import com.newasia.management.dal.item.entitys.ItemSpecValue;
import com.newasia.management.dal.item.entitys.ItemSpecValueExample;
import com.newasia.management.dal.item.persistence.ItemSkuMapper;
import com.newasia.management.dal.item.persistence.ItemSpecMapper;
import com.newasia.management.dal.item.persistence.ItemSpecValueMapper;
import com.newasia.management.dal.item.persistence.ItemSpuMapper;
import com.newasia.management.dto.productSpec.*;
import com.newasia.management.dto.productSpecValue.*;
import com.newasia.management.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;


@Slf4j
@Service
public class ManagementSpecImpl implements IManagementSpecService {

    @Autowired
    ItemSpecMapper itemSpecMapper;

    @Autowired
    ItemSpecConverter itemSpecConverter;

    @Autowired
    ItemSpecValueMapper itemSpecValueMapper;



    @Autowired
    ItemSkuMapper itemSkuMapper;

    @Autowired
    ItemSpuMapper itemSpuMapper;



    /*********************商品规格样式**********************/
    /**
     * 增加商品规格样式-父
     * @return
     */

    @Override
    public CreateItemSpecResponse createItemSpec(CreateItemSpecRequest request) {
        CreateItemSpecResponse response = new CreateItemSpecResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
//            request.requestCheck();
//            ItemSpec spec = itemSpecConverter.req2spec(request);
//            Date date = new Date();
//            spec.setCreateTime(date);
//            spec.setUpdateTime(date);
//            itemSpecMapper.insert(spec);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }

    /**
     * 增加商品规格样式-子
     * @return
     */
    @Override
    public CreateSpecValueResponse createSpecValue(CreateSpecValueRequest request) {
        CreateSpecValueResponse response = new CreateSpecValueResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
////            ItemSpecValue specValue = itemSpecConverter.req2spec(request);
//
//            ItemSpu itemSpu = itemSpuMapper.selectByPrimaryKey(request.getSpuId());
//            Date date = new Date();
//
//            ItemSpecValue specValue=new ItemSpecValue();
//            specValue.setSpecValue(request.getSpecValue());
//            specValue.setSpecId(request.getSpecId());
//            specValue.setCreateTime(date);
//            specValue.setUpdateTime(date);
//            itemSpecValueMapper.insert(specValue);
//
//            Long valueId = specValue.getValueId();
//
////            ItemSpecValueExample example=new ItemSpecValueExample();
////            ItemSpecValueExample.Criteria criteria = example.createCriteria();
////            criteria.andSpecIdEqualTo(request.getSpuId());
////            criteria.andSpecIdNotEqualTo(request.getSpecId());
////
////            itemSpecValueMapper.selectByExample(example);
//
//
//            SpuSpecExample example=new SpuSpecExample();
//            SpuSpecExample.Criteria criteria1 = example.createCriteria();
//            criteria1.andSpuIdEqualTo(request.getSpuId());
//            criteria1.andSpecIdNotEqualTo(request.getSpecId());
//            List<SpuSpec> spuSpecs = spuSpecMapper.selectByExample(example);
//
//            for (SpuSpec s:spuSpecs) {
//                ItemSpecValueExample e=new ItemSpecValueExample();
//                e.createCriteria().andSpecIdEqualTo(s.getSpecId());
//                List<ItemSpecValue> itemSpecValues = itemSpecValueMapper.selectByExample(e);
//                s.setSpecValues(itemSpecValues);
//            }
//
//
//            int parentSize = spuSpecs.size();
//
//            String[][] data = new String[parentSize][];
//            for (int i=0;i<parentSize;i++){
//                int childSize = spuSpecs.get(i).getSpecValues().size();
//                data[i]=new String[childSize];
//                for (int j=0;j<childSize;j++){
//                    data[i][j]=spuSpecs.get(i).getSpecValues().get(j).getValueId().toString();
//                }
//            }
//            String[] strings = doInsertSku(data);
//
//            ItemSpecValueExample e =new ItemSpecValueExample();
//            ItemSpecValueExample.Criteria criteria = e.createCriteria();
//            criteria.andSpecIdEqualTo((long) 5);
//            List<ItemSpecValue> agentList = itemSpecValueMapper.selectByExample(e);
//
//
//            //创建sku
//            for (ItemSpecValue dto:agentList){
//
//                for (String i : strings) {
//                    log.info(i);
//                    ItemSku sku =new ItemSku();
//                    sku.setSpuId(request.getSpuId());
//                    sku.setLimitNum(999);
//                    sku.setNum(999);
//                    sku.setSkuPrice( BigDecimal.valueOf(99999));
//                    String img="[\""+  itemSpu.getImage()+"\"]";
//                    sku.setSkuImage(img);
//                    sku.setStatus(0);
//
//                    Date d= new Date();
//                    sku.setCreateTime(d);
//
//                    itemSkuMapper.insert(sku);
//                    Long skuId = sku.getSkuId();
//                    String[] sortArry = i.split(",");
//
//
//                    //创建sku_spec_value
//                    for (String item: sortArry) {
//
//                        SkuSpecValue skuSpecValue =new SkuSpecValue();
//                        skuSpecValue.setSkuId(skuId);
//                        skuSpecValue.setCreateTime(date);
//                        skuSpecValue.setStatus(1);
//                        skuSpecValue.setValueId(Long.valueOf(item));
//                        skuSpecValueMapper.insert(skuSpecValue);
//
//                    }
//
//
//                    SkuSpecValue agent =new SkuSpecValue();
//                    agent.setSkuId(skuId);
//                    agent.setCreateTime(date);
//                    agent.setStatus(1);
//                    agent.setValueId(dto.getValueId());
//                    skuSpecValueMapper.insert(agent);
//
//                    SkuSpecValue data2 =new SkuSpecValue();
//                    data2.setSkuId(skuId);
//                    data2.setCreateTime(date);
//                    data2.setStatus(1);
//                    data2.setValueId(valueId);
//                    skuSpecValueMapper.insert(data2);
//                }
//            }
//
//
//            ItemSpecExample example = new ItemSpecExample();
//            ItemSpecExample.Criteria criteria = example.createCriteria();
//            criteria.andIdEqualTo(specValue.getSpecId());
//            long count = itemSpecMapper.countByExample(example);
//            if(count<1){
//                throw new ValidateException(
//                        ManagementRetCode.NOT_EXIST_ITEM_SPEC .getCode(),
//                        ManagementRetCode.NOT_EXIST_ITEM_SPEC .getMessage()
//                );
//            }


        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }

//    private String[] doInsertSku(String[][] spuSpecList) {
//
//
//        int length = spuSpecList.length;
//        if(length>=2){
//            int length1 = spuSpecList[0].length;
//            int length2 = spuSpecList[1].length;
//            String[] ss=new String[length1*length2];
//            int index=0;
//            for (int i = 0; i < length1; i++) {
//                for (int j = 0; j < length2; j++) {
//
//                    ss[index] =spuSpecList[0][i]+","+spuSpecList[1][j];
////
//                    index++;
//                }
//            }
//            String[][] newArr = new String[length-1][];
//            for (int i = 2; i < spuSpecList.length; i++) {
//                newArr[i - 1]=new String[spuSpecList[i].length];
//                for (int j = 0; j < newArr[i - 1].length; j++){
//                    newArr[i - 1][j] =spuSpecList[i][j] ;
//                }
//
//            }
//            newArr[0]=ss;
//            return doInsertSku(newArr);
//        }else {
//            return spuSpecList[0];
//        }
//
//    }


    /**
     * 删除商品规格样式-父
     * @return
     */
    @Override
    public DeleteItemSpecResponse deleteitemSpec(Long specId) {
        DeleteItemSpecResponse response = new DeleteItemSpecResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
//            if(specId==5){
//                throw new ValidateException(
//                        ManagementRetCode.CAN_NOT_DELETE.getCode(),
//                        ManagementRetCode.CAN_NOT_DELETE .getMessage()
//                );
//            }
//            ItemSpecValueExample example = new ItemSpecValueExample();
//            ItemSpecValueExample.Criteria criteria = example.createCriteria();
//            criteria.andSpecIdEqualTo(specId);
//            long count = itemSpecValueMapper.countByExample(example);
//            if(count>0){
//                throw new ValidateException(
//                        ManagementRetCode.EXIST_ITEM_SPEC .getCode(),
//                        ManagementRetCode.EXIST_ITEM_SPEC .getMessage()
//                );
//            }
//
//            itemSpecMapper.deleteByPrimaryKey(specId);

        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }


    /**
     * 删除商品规格样式-子
     * @return
     */
    @Override
    public DeleteSpecValueResponse deleteSpecValue(Long specValueId) {
        DeleteSpecValueResponse response = new DeleteSpecValueResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{

//            SkuSpecValueExample example = new SkuSpecValueExample();
//            SkuSpecValueExample.Criteria criteria = example.createCriteria();
//            criteria.andValueIdEqualTo(specValueId);
//            long count = skuSpecValueMapper.countByExample(example);
//            if(count>0){
//                throw new ValidateException(
//                        ManagementRetCode.EXIST_ITEM_SPEC .getCode(),
//                        ManagementRetCode.EXIST_ITEM_SPEC .getMessage()
//                );
//            }
//
//            itemSpecValueMapper.deleteByPrimaryKey(specValueId);

        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }




    /**
     * 修改商品规格样式--父
     * @return
     */
    @Override
    public ChangeItemSpecResponse changeItemSpec(Long specId, ChangeItemSpecRequest request) {
        ChangeItemSpecResponse response = new ChangeItemSpecResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            request.requestCheck();
            ItemSpecExample example = new ItemSpecExample();
            example.createCriteria().andIdEqualTo(specId);

            ItemSpec spec = itemSpecConverter.chageReq2spec(request);
            Date date = new Date();
            spec.setUpdateTime(date);

            itemSpecMapper.updateByExampleSelective(spec,example);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }


    /**
     * 修改商品规格样式--子
     * @return
     */
    @Override
    public ChangeSpecValueResponse changeSpecValue(Long specValueId, ChangeSpecValueRequest request) {
        ChangeSpecValueResponse response = new ChangeSpecValueResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{

            request.requestCheck();

            ItemSpecValueExample example = new ItemSpecValueExample();
            example.createCriteria().andValueIdEqualTo(specValueId);

            ItemSpecValue spec = itemSpecConverter.chageReq2specValue(request);
            Date date = new Date();
            spec.setUpdateTime(date);
            itemSpecValueMapper.updateByExampleSelective(spec,example);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }


    /**
     * 获取商品规格样式
     * @return
     */
    @Override
    public GetSpecResponse getSpecList(String key) {
        GetSpecResponse response = new GetSpecResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            ItemSpecExample example=new ItemSpecExample();
            if(key!=null){
                example.createCriteria().andSpecNameLike("%"+ key+"%");
            }
            List<ItemSpec> itemSpecs = itemSpecMapper.selectByExample(example);



            List<SpecDto> SpecDtoList=new ArrayList<>();
            for (ItemSpec item:itemSpecs) {
                SpecDto dto =new SpecDto();
                dto.setSpecId(item.getId());
                dto.setSpecName(item.getSpecName());
                dto.setSpecStatus(item.getStatus());


//                ItemSpecValueExample e=new ItemSpecValueExample();
//                e.createCriteria().andSpecIdEqualTo(item.getId());
//                List<ItemSpecValue> itemSpecValues = itemSpecValueMapper.selectByExample(e);
//                List<SpecValueDto> child=new ArrayList<>();
//                for (ItemSpecValue values:itemSpecValues) {
//                    SpecValueDto value =new SpecValueDto();
//                    value.setValueId(values.getValueId());
//                    value.setSpecValue(values.getSpecValue());
//                }
//                dto.setChildren(child);
                SpecDtoList.add(dto);
            }
            response.setSpecDtoList(SpecDtoList);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }


    /**
     * 获取商品子规格样式
     * @return
     */
    @Override
    public GetSpecValueResponse getSpecValueList(Long id) {
        GetSpecValueResponse response = new GetSpecValueResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            ItemSpecValueExample example=new ItemSpecValueExample();
            if(id!=null){
                example.createCriteria().andSpecIdEqualTo(id);
            }else{
                throw new ValidateException(
                        ManagementRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                        ManagementRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
            }

            List<ItemSpecValue> itemSpecValues = itemSpecValueMapper.selectByExample(example);

            List<SpecValueDto> list=new ArrayList<>();

                for (ItemSpecValue values:itemSpecValues) {
                    SpecValueDto value =new SpecValueDto();
                    value.setValueId(values.getValueId());
                    value.setSpecValue(values.getSpecValue());
                    list.add(value);
                }

            response.setSpecValueList(list);
        }catch (Exception e){
            log.error("ManagementItemCatServiceImpl.getItemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            e.printStackTrace();
            return response;
        }
        return response;
    }


}
